﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HotelManagement.DAO;
using HotelManagement.DTO;

namespace HotelManagement.BLO
{
    class RentBLO
    {
        private RentDAO dao;
        public RentBLO()
        {
            dao = new RentDAO();
        }
        public bool Insert(RentDTO rent)
        {
            return dao.Insert(rent);
        }
        public bool Update(RentDTO rent)
        {
            return dao.Update(rent);
        }
        public DataTable Select()
        {
            return dao.Select();
        }
        public bool BookIn(RentDTO rent)
        {
            RoomDAO daoR = new RoomDAO();
            RoomDTO room = daoR.SelectRoomFromID(rent.RoomID);
            room.Status = HotelManagement.CheckInput.STATUS_BOOK_IN;
            room.LastOwner = rent.CustomerID;
            if (dao.Insert(rent))
            {
                return daoR.UpdateWhenRent(room);
            }
            return false;
        }

        public bool UpdateStatus(RentDTO rent)
        {
            return dao.UpdateStatus(rent);
        }
        /*this function describes the rent room business logic
         * Rent room is a logical business that there is a rent in table RENT and at least 
         * one rentdetail in RENTDETAIL
         * So, RentRoom() function adds one Rent and at least one RentDetail 
         * Check table Rent out if there is th
        */
        // public bool RentRoom()
        //nhận phòng: 
        //có thể nhận một phòng trống hoặc là phòng đã được đặt trước
        


        public RentDTO SelectFromRoomIDAndStatus(int pRoomID, int pStatus)
        {
            return dao.SelectFromRoomIDAndStatus(pRoomID, pStatus);
        }
        //Hàm này trả về những phiếu đặt và phiếu nhận phòng
        public DataTable GetBookInAndCheckInRent()
        {
            return dao.GetBookInAndCheckInRent();
        }
        public RentDTO SelectRentFromID(int pRentID)
        {
            if (pRentID==0)
            {
                return null;
            }
            return dao.SelectRentFromID(pRentID);
        }
        public RoomDTO SelectRoomOfARent(int pRentID)
        {
            if (pRentID==0)
            {
                return null;
            }
            DataTable tbl = dao.SelectRoomOfARent(pRentID);
            if (tbl!=null)
            {
                RoomDTO room = new RoomDTO();
                room.RoomID = int.Parse(tbl.Rows[0]["RoomID"].ToString());
                room.RoomName = tbl.Rows[0]["RoomName"].ToString();
                room.RoomTypeID = int.Parse(tbl.Rows[0]["RoomTypeID"].ToString());
                room.NumOfBed = int.Parse(tbl.Rows[0]["NumOfBed"].ToString());
                room.NumOfPerson = int.Parse(tbl.Rows[0]["NumOfPerson"].ToString());
                room.Direction = tbl.Rows[0]["Direction"].ToString();
                room.Views = tbl.Rows[0]["Views"].ToString();
                room.Status = int.Parse(tbl.Rows[0]["Status"].ToString());
                room.LastOwner = int.Parse(tbl.Rows[0]["LastOwner"].ToString());
                return room;
            }
            return null;
        }
    }
}
